home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_07_01 / v7n1106a.txt < prev    next >
Text File  |  1988-10-26  |  5KB  |  189 lines

  1.  
  2.  
  3. The following CROBOTS program is provided as an example.
  4.  
  5.  
  6. /* sniper */
  7. /* strategy: since a scan of the entire battlefield can be done in 90 */
  8. /* degrees from a corner, sniper can scan the field quickly. */
  9.  
  10. /* external variables, that can be used by any function */
  11. int corner;           /* current corner 0, 1, 2, or 2 */
  12. int c1x, c1y;         /* corner 1 x and y */
  13. int c2x, c2y;         /*   "    2 "  "  " */
  14. int c3x, c3y;         /*   "    3 "  "  " */
  15. int c4x, c4y;         /*   "    4 "  "  " */
  16. int s1, s2, s3, s4;   /* starting scan position for corner 1 - 4 */
  17. int sc;               /* current scan start */
  18. int d;                /* last damage check */
  19.  
  20. /* main */
  21. main()
  22. {
  23. int closest;        /* check for targets in range */
  24. int range;          /* range to target */
  25. int dir;            /* scan direction */
  26.  
  27. /* initialize the corner info */
  28. /* x and y location of a corner, and starting scan degree */
  29. c1x = 10;  c1y = 10;  s1 = 0;
  30. c2x = 10;  c2y = 990; s2 = 270;
  31. c3x = 990; c3y = 990; s3 = 180;
  32. c4x = 990; c4y = 10;  s4 = 90;
  33. closest = 9999;
  34. new_corner();       /* start at a random corner */ 
  35. d = damage();       /* get current damage */
  36. dir = sc;           /* starting scan direction */ 
  37.  
  38. while (1) {         /* loop is executed forever */
  39.  
  40.   while (dir < sc + 90) {  /* scan through 90 degree range */
  41. range = scan(dir,1);   /* look at a direction */
  42. if (range <= 700 && range > 0) { 
  43. while (range > 0) {    /* keep firing while in range */
  44.   closest = range;     /* set closest flag */
  45.   cannon(dir,range);   /* fire! */
  46.   range = scan(dir,1); /* check target again */
  47.   if (d + 15 > damage())  /* sustained several hits, */ 
  48.     range = 0;            /* goto new corner */
  49. }
  50. dir -= 10;             /* back up scan, in case */
  51. }
  52.  
  53. dir += 2;                /* increment scan */
  54. if (d != damage()) {     /* check for damage incurred */
  55. new_corner();          /* we're hit, move now */
  56. d = damage();
  57. dir = sc;
  58. }
  59.   }
  60.  
  61.   if (closest == 9999) {       /* check for any targets in range */
  62. new_corner();             /* nothing, move to new corner */
  63. d = damage();
  64. dir = sc;
  65.   } else                      /* targets in range, resume */
  66. dir = sc;
  67.   closest = 9999;
  68.  
  69. }  /* end of main */
  70.  
  71. /* new corner function to move to a different corner */
  72. new_corner() {
  73. int x, y;
  74. int angle;
  75. int new;
  76.  
  77. new = rand(4);           /* pick a random corner */
  78. if (new == corner)       /* but make it different than the */
  79.   corner = (new + 1) % 4;/* current corner */
  80. else
  81.   corner = new;
  82. if (corner == 0) {       /* set new x,y and scan start */
  83.   x = c1x; 
  84.   y = c1y; 
  85.   sc = s1;
  86. if (corner == 1) {
  87.   x = c2x; 
  88.   y = c2y; 
  89.   sc = s2;
  90. }
  91. if (corner == 2) {
  92.   x = c3x; 
  93.   y = c3y; 
  94.   sc = s3;
  95. }
  96. if (corner == 3) { 
  97.   x = c4x; 
  98.   y = c4y; 
  99.   sc = s4;
  100. }
  101.  
  102. /* find the heading we need to get to the desired corner */
  103. angle = plot_course(x,y);
  104.  
  105. /* start drive train, full speed */
  106. drive(angle,100);
  107.  
  108. /* keep traveling until we are within 100 meters */
  109. /* speed is checked in case we run into wall, other robot */
  110. /* not terribly great, since were are doing nothing while moving */
  111.  
  112. while (distance(loc_x(),loc_y(),x,y) > 100 && speed() > 0)
  113.   ;
  114.  
  115. /* cut speed, and creep the rest of the way */
  116.  
  117. drive(angle,20);
  118. while (distance(loc_x(),loc_y(),x,y) > 10 && speed() > 0)
  119.   ;
  120.  
  121. /* stop drive, should coast in the rest of the way */
  122. drive(angle,0); 
  123. }  /* end of new_corner */
  124.  
  125. /* classical pythagorean distance formula */
  126. distance(x1,y1,x2,y2)
  127. int x1;
  128. int y1;
  129. int x2;
  130. int y2;
  131. {
  132. int x, y;
  133.  
  134. x = x1 - x2;
  135. y = y1 - y2;
  136. d = sqrt((x*x) + (y*y));
  137. return(d);
  138. }
  139.  
  140. /* plot course function, return degree heading to */
  141. /* reach destination x, y; uses atan() trig function */
  142. plot_course(xx,yy)
  143. int xx, yy;
  144. {
  145. int d;
  146. int x,y;
  147. int scale;
  148. int curx, cury;
  149.  
  150. scale = 100000;  /* scale for trig functions */
  151. curx = loc_x();  /* get current location */
  152. cury = loc_y();
  153. x = curx - xx;
  154. y = cury - yy;
  155.  
  156. /* atan only returns -90 to +90, so figure out how to use */
  157. /* the atan() value */
  158.  
  159. if (x == 0) {      /* x is zero, we either move due north or south */
  160.   if (yy > cury)
  161. d = 90;        /* north */
  162.   else
  163. d = 270;       /* south */
  164. } else {
  165.   if (yy < cury) {
  166. if (xx > curx)
  167. d = 360 + atan((scale * y) / x);  /* south-east, quadrant 4 */
  168. else
  169. d = 180 + atan((scale * y) / x);  /* south-west, quadrant 3 */ 
  170.   } else {
  171. if (xx > curx)
  172. d = atan((scale * y) / x);        /* north-east, quadrant 1 */
  173. else
  174. d = 180 + atan((scale * y) / x);  /* north-west, quadrant 2 */
  175.   }
  176. }
  177. return (d);
  178. }
  179.  
  180. Notes:  The distance() and plot_course() routines are
  181. quite handy. Save them for your programs. Also, note that the
  182. main scan routine will "back up" a few degrees after a target has
  183. been found and fired upon. This should catch robots trying to
  184. flee away from the direction you are scanning. If the target
  185. moves the other way, the normal scan increment will find it.
  186.  
  187.